/*
 * TODO
 */
package com.xtrip.security.util;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import com.xtrip.common.BasicConstant;

/**
 * @project tclpay
 * @author shiming.yao
 * @time 2013-3-29
 */
public class SecurityUtil {

	
	public static String md5(String src){
		String encode = BasicConstant.ENCODE_UTF8;
		return md5(src, encode);
	}
	private static String md5(String src,String encode){
		char[] chars = new char[]{'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
		try {
			MessageDigest md = MessageDigest.getInstance("md5");
			byte[] srbytes = src.getBytes(encode);
			md.update(srbytes);
			byte[] encbyte = md.digest();
			int length = encbyte.length;
			char[] result = new char[length * 2];
			int x = 0;
			for(int i=0;i<length;i++){
				byte b = encbyte[i];
				result[x++] = chars[b>>>4&0x0f];
				result[x++] = chars[b&0x0f];
			}
			return new String(result);
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	//验签
	public static boolean valid(String pwd , String encStr){
		return md5(pwd).equals(encStr);
	}
}
